<?php

class Base_Controller extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
        define("SITEURL", 'http://140.143.56.78:7777/');
        define("BASEURL", 'http://140.143.56.78:7777/');
        define("STATICURL",BASEURL."Public/statics/");
        define("UPLOADURL",BASEURL.'uploads');
        //define("VIEWPATH",BASEURL."views/");
        $this->init();
    }
    public function init()
    {
        $currentClass = $this->router->fetch_class();
        if($currentClass != "login"){
            $login_status  = $this->check_login_status();
            if (empty($login_status)){
                redirect(BASEURL.'/login');die;
            }
        }
    }
    /**
     * 登陆状态检测
     */
    public function check_login_status()
    {
        $this->load->model('Admin_user_model', 'admin_user');
        return $this->admin_user->get_login_status();
    }



    function create_page_links($base_url,$total_cnt,$limit)
    {
        $this->load->library('Pagination');
        $conf_page	= array('base_url'=>$base_url,
                              'total_rows'=>$total_cnt,
                              'per_page'=>$limit,
                              'page_query_string'=>true,
                              'first_tag_open'=>'<span class="page_btn">',
                              'first_tag_close'=>'</span>',
                              'last_tag_open'=>'<span  class="page_btn">',
                              'last_tag_close'=>'</span>',
                              'prev_tag_open'=>'<span  class="page_btn">',
                              'prev_tag_close'=>'</span>',
                              'next_tag_open'=>'<span  class="page_btn">',
                              'next_tag_close'=>'</span>',
                              'num_tag_open'=>'<span  class="page_btn">',
                              'num_tag_close'=>'</span>',
                              'cur_tag_open'=>'<span  class="page_btn current">',
                              'cur_tag_close'=>'</span>',
                              'first_link'=>'首页',
                              'prev_link'=>'上页',
                              'next_link'=>'下页',
                              'last_link'=>'末页',
                              'num_links'=>5,
                              'total_lang'=>'共',
                              'pages_lang'=>'页',
                              'tiao_lang'=>'条');
        $this->pagination->initialize($conf_page);
        $page_links	= $this->pagination->create_links();
        return $page_links;
    }

    protected function json(int $code, $message, $data = null)
    {
        header('Content-Type: application/json; charset=utf8');
        exit(json_encode([
            'code'    => $code,
            'message' => $message,
            'data'    => $data ?: new stdClass(),
        ], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES));
    }

    protected function success($data = null)
    {
        $this->json(1, 'success', $data);
    }

    protected function error($message, $data = null)
    {
        $this->json(0, $message, $data);
    }


    protected function getQuery($name, $default = null, $xss = true)
    {
        $param = $this->input->get($name, $xss);
        if (!isset($param)) {
            return $default;
        }
        if (is_array($param)) {
            foreach ($param as &$value) {
                $value = strip_tags($value);
            }
        } else {
            $param = strip_tags($param);
        }
        return $param;
    }

    protected function getPost($name, $default = null, $xss = true)
    {
        $param = $this->input->post($name, $xss);
        if (!isset($param)) {
            return $default;
        }
        if (is_array($param)) {
            foreach ($param as &$value) {
                $value = strip_tags($value);
            }
        } else {
            $param = strip_tags($param);
        }
        return $param;
    }

    protected function defineUser()
    {
        if(!$userId = $_SESSION['pgc_user_id']['user_id'] ?? 0){
            define("ACCOUNT_ID",2056);
            return true;
        }
        define("ACCOUNT_ID",2056);
    }

    public function output($code, $msg, $data = '', $error = '')
    {
        header('Content-Type:application/json; charset=utf-8');
        $output            = array();
        $output['success'] = ($code == 200) ? true : false;
        $output['code']    = $code;
        $output['message'] = $msg;
        $output['data']    = $data;
        $error ? $output['error'] = $error : '';
        //var_dump($output);
        echo json_encode($output, true);

    }
}
